package com.yicong.ants.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cchao.simplelib.core.PrefHelper;
import com.cchao.simplelib.ui.activity.BaseActivity;
import com.cchao.simplelib.ui.adapter.StatefulBindMultiQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.R;
import com.yicong.ants.bean.home.ScenicItemWrapper;
import com.yicong.ants.bean.scenic.ScenicItem;
import com.yicong.ants.databinding.ActivityBasicBinding;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.config.ZzConfigs;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BasicActivity extends BaseActivity {

    private ActivityBasicBinding mDataBind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.themeColor));
        mDataBind = ActivityBasicBinding.inflate(LayoutInflater.from(this));
        setContentView(mDataBind.getRoot());
        mDataBind.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToStart();
            }
        });

        String string = getAssetsString();
        List<ScenicItem> data = new Gson().fromJson(string, new TypeToken<List<ScenicItem>>() {
        }.getType());
        List<ScenicItemWrapper> list = new ArrayList<>();
        for (ScenicItem datum : data) {
            list.add(ScenicItemWrapper.of(0, datum));
        }
        initAdapter(list);
    }

    private String getAssetsString() {
        String str;
        try (InputStream inputStream = this.getAssets().open("basicjson.txt")) {
            int length = inputStream.available();
            byte[] bytes = new byte[length];
            int i = inputStream.read(bytes);
            str = new String(bytes, StandardCharsets.UTF_8).trim();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return str;
    }

    private StatefulBindMultiQuickAdapter<ScenicItemWrapper> mAdapter;

    private void initAdapter(List<ScenicItemWrapper> list) {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindMultiQuickAdapter<ScenicItemWrapper>(null) {

            @Override
            public Map<Integer, Integer> getTypeLayoutMap() {
                Map<Integer, Integer> map = new HashMap<>(8);
                map.put(0, R.layout.scenic_item);
                return map;
            }

            @Override
            protected void convert(DataBindViewHolder helper, ScenicItemWrapper data) {
                convertScenic(helper, (ScenicItem) data.getObject());
            }

            @Override
            public void loadPageData(int page) {
            }

            private void convertScenic(DataBindViewHolder helper, ScenicItem item) {
                helper.getBinding().setVariable(BR.bean, item);
            }
        };
        mAdapter.bindToRecyclerView(mDataBind.recycler);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            goToStart();
        });
        mAdapter.setNewData(list);
        mAdapter.loadMoreEnd();
    }

    private void goToStart() {
        Dialogs.showPrivacyAllow(this,true, new Runnable() {
            @Override
            public void run() {
                PrefHelper.putIntCommit("confirm_app" , getPrivacyAgreeKey());

                finish();
                startActivity(new Intent(BasicActivity.this, AppStartActivity.class));
            }
        });
    }

    private int getPrivacyAgreeKey() {
        return ZzConfigs.getClientConf().getWebUrl("ant_privacy").version;
    }
}